package com.ygo.feihua.fragment;
import android.os.*;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.support.v4.widget.*;
import android.support.v7.widget.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.view.animation.*;
import android.widget.*;
import android.widget.Toolbar.*;
import cn.bmob.v3.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.Management.*;
import com.ygo.feihua.adapter.*;
import com.ygo.feihua.console.*;
import com.ygo.feihua.task.*;
import com.ygo.feihua.util.*;
import com.ygo.feihua.view.*;
import java.lang.ref.*;
import java.util.*;

import android.support.v4.util.ArrayMap;
import android.view.View.OnClickListener;
import com.ygo.feihua.*;

public class FragmentZhuye extends Fragment implements OnClickListener,Task, SwipeRefreshLayout.OnRefreshListener
{

	private List<Map<String,Object>> ggl=new ArrayList<Map<String,Object>>();
    
    
	private LinearLayout  zy_ggli;
	private LinearLayout llAdvertiseBoard;
    private AutoTextView mAutoTextView;
	private CardView main_gg;
	
    private Handler mEventHandler;
    private int mLoopCount = 0;
	
	private String btt="",nrr="";
    
	private Gongju gj;
	private DialogUtils du;
	
	private TaskConsole tac;
	private static final int INIT_GONGGAO = 0;
	private static final int INIT_GUANGGAO = 1;
	private static final int INIT_ZIXUN=2;
	private List<GuangGao> guanggaoList;
	
	private RecyclerView zx_rec;
	private SwipeRefreshLayout zx_sw;
	private List<ZiXun> ziXuns;
	private UserManagement um;
	private ZixunAdapter adpz;
	private String TAG="FragmentZhuye";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		// TODO: Implement this method
		View view=inflater.inflate(R.layout.fragment_zhuye, null);
		initViews(view);
				
		cshView(view);
		
		return view;
	}
	
	
	
	
	@Override
	public void onRefresh()
	{
		cshZx();
		// TODO: Implement this method
	}

	@Override
	public void onClick(View p1)
	{
		switch (p1.getId()){
			case R.id.zy_gg:
				if(ggl.size()!=0){
					View[] v=du.dialogt1(true,btt,nrr);
					Button b1=(Button) v[0];
					b1.setText("确定");
					b1.setOnClickListener(new OnClickListener(){

							@Override
							public void onClick(View p1)
							{
								du.dis();
								// TODO: Implement this method
							}
						});
				}
				break;	
		}
		// TODO: Implement this method
	}
	
	@Override
	public void execute(int number)
	{
		han.sendEmptyMessage(number);
		// TODO: Implement this method
	}
	
	Handler han=new Handler(){

		
		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			switch(msg.what){
				case INIT_GONGGAO:
					if(ggl.size()!=0){
						mEventHandler = new EventHandler(FragmentZhuye.this);
						//0.05秒后开始滑动
						mEventHandler.sendEmptyMessageDelayed(1, 50);
					}
					break;
				case INIT_GUANGGAO:
					Log.e(TAG,"开始");
					llAdvertiseBoard.addView(new Advertisements(getActivity(), true,  3000,0).initView((List<Object>)guanggaoList));
					Log.e(TAG,"结束");
					break;
				case INIT_ZIXUN:
					adpz.notifyDataSetChanged();
					break;
			}
		}
	};
	
	private void cshView(View v){

		zx_rec=(RecyclerView) v.findViewById(R.id.zx_rec);
		zx_sw=(SwipeRefreshLayout) v.findViewById(R.id.zx_sw);
		ziXuns=new ArrayList<ZiXun>();
		zx_rec.setLayoutManager(new LinearLayoutManager(getActivity()));
		adpz=new ZixunAdapter(getActivity(),ziXuns);
		zx_rec.setAdapter(adpz);
		zx_sw.setColorSchemeResources(R.color.colorMain);
		zx_sw.setOnRefreshListener(this);
		//手动调用,通知系统去测量
        zx_sw.measure(0,0);
		zx_sw.setRefreshing(true);
		

		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		um=UserManagement.getDx();
		onRefresh();
		cshZx();
	}
	
	
	
	private void cshZx(){

		BmobQuery<ZiXun> zx=new BmobQuery<ZiXun>();
		zx.order("-createdAt");
		zx.include("zs_message");
		zx.findObjects(new FindListener<ZiXun>(){

				@Override
				public void done(List<ZiXun> p1, BmobException p2)
				{
					zx_sw.setRefreshing(false);
					if(p2==null){
						ziXuns.clear();
						for(ZiXun zx:p1){
							ziXuns.add(zx);
						}
						tac.addTask(FragmentZhuye.this,INIT_ZIXUN);
					}else{
						gj.toastBmobException(zx_rec,"资讯加载失败,下拉重新加载",p2);
					}
					
					// TODO: Implement this method
				}
			});

	}
	
	
	private void initViews(View v) {
		
		
		zy_ggli=(LinearLayout) v.findViewById(R.id.zy_ggli);
		mAutoTextView = (AutoTextView) v.findViewById(R.id.zy_gg);
		llAdvertiseBoard = (LinearLayout) v.findViewById(R.id.llAdvertiseBoard);
		main_gg=(CardView) v.findViewById(R.id.main_gg);
		
		tac=TaskConsole.getDx();
		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		
		mAutoTextView.setText("公告初始化中");
		mAutoTextView.setOnClickListener(this);
		
		llAdvertiseBoard.post(new Runnable() {
				@Override
				public void run() {
					//设置广告高度为宽度的1/3,即长宽比为3:1
					int height=llAdvertiseBoard.getWidth()/3;
					//main_gg.setVisibility(View.GONE);
					llAdvertiseBoard.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,height));
				}
			});
		
		BmobQuery<GuangGao> bq=new BmobQuery<GuangGao>();
		//只查询10条
		bq.setLimit(10);
		bq.order("-createdAt");
		bq.addWhereEqualTo("gg_yc",1);
		bq.findObjects(new FindListener<GuangGao>(){

				
				@Override
				public void done(List<GuangGao> p1, BmobException p2)
				{
					if(p2==null){
						guanggaoList=p1;
						tac.addTask(FragmentZhuye.this,INIT_GUANGGAO);
					}
					// TODO: Implement this method
				}
			});		
		
		BmobQuery<GongGao> gg=new BmobQuery<GongGao>();
		gg.setLimit(50);
		gg.order("-createdAt");
		gg.findObjects(new FindListener<GongGao>(){

				
				@Override
				public void done(List<GongGao> p1, BmobException p2)
				{
					if(p2==null){
						for(GongGao g:p1){
							Map<String,Object> map=new ArrayMap<String,Object>();
							map.put("bt",g.getBiaoti());
							map.put("nr",g.getNeirong());
							ggl.add(map);
						}
						tac.addTask(FragmentZhuye.this,INIT_GONGGAO);
						
					}
					// TODO: Implement this method
				}
			});
	}
	
	
    public static class EventHandler extends Handler {

        private WeakReference<FragmentZhuye> wr;

        public EventHandler(FragmentZhuye r) {
            wr = new WeakReference<FragmentZhuye>(r);
        }

        @Override
        public void handleMessage(Message msg) {
            FragmentZhuye activity = wr.get();
            switch (msg.what) {
                case 1:
                    activity.mAutoTextView.next();
					activity.btt=(String)activity.ggl.get(activity.mLoopCount/*activity.zt*/).get("bt");
					activity.nrr=(String)activity.ggl.get(activity.mLoopCount).get("nr");
                    activity.mAutoTextView.setText(activity.btt);
					if(activity.ggl.size()!=1){

						activity.mEventHandler.sendEmptyMessageDelayed(1, 5000);

						if(activity.mLoopCount+1!=activity.ggl.size()){
							activity.mLoopCount ++;
						}else{
							activity.mLoopCount=0;
						}
						}
                    break;
            }
        }
    }
	
	
	
}
